package com.albon.alarm;


import java.util.Calendar;

import com.albon.soccer.DatabaseHelper;
import com.albon.soccer.Soccer;
import com.albon.sportsalarm.R;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SoccerDeviceBooting extends BroadcastReceiver implements StaticVariable{
	
	public static final String TAG = "SoccerDeviceBooting";
	private Context mContext = null;
	private String action = "";
	private Soccer soccer;
	
	private  String[] notification_time_array_value =  {}; 
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		mContext = context;
		notification_time_array_value = context.getResources().getStringArray(R.array.notification_time_array_value);
		action = intent.getAction();
		if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
			loadSoccerDBAlarm(context);
		}
	}

	private void loadSoccerDBAlarm(Context context) {
		// TODO Auto-generated method stub
		SQLiteDatabase databse = null;

		if(databse == null){
			databse = context.openOrCreateDatabase(DatabaseHelper.DATABASE_NAME, SQLiteDatabase.CREATE_IF_NECESSARY, null);
		}
		String stringWhere = " WHERE " + DatabaseHelper.DATABASE_COLUMN_ENABLED + " = 'true'" ;
		Cursor cursor = null;
		try {
			cursor = databse.rawQuery("SELECT "
					+ DatabaseHelper.DATABASE_COLUMN_ID + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ENABLED + ", "
					+ DatabaseHelper.DATABASE_COLUMN_REAL_DATE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_REAL_TIME + ", "
					+ DatabaseHelper.DATABASE_COLUMN_COMPARE_DATE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SETTING_YEAR + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SETTING_MONTH + ", "
					+ DatabaseHelper.DATABASE_COLUMN_REAGUE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_STADIUM + ", "
					+ DatabaseHelper.DATABASE_COLUMN_A_TEAM + ", "
					+ DatabaseHelper.DATABASE_COLUMN_B_TEAM + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SCORE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_TITLE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_RINGTONE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_VOLUME + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_VIBRATE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_AUTO_OFF_TIME + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SMS + ", "
					+ DatabaseHelper.DATABASE_COLUMN_VIDEO + ", "
					+ DatabaseHelper.DATABASE_COLUMN_RECORD + ", "						
					+ DatabaseHelper.DATABASE_COLUMN_HIGHLIGHT + ", "						
					+ DatabaseHelper.DATABASE_COLUMN_POWER + ", "													
					+ DatabaseHelper.DATABASE_COLUMN_NOTIFICATION + ", "													
					+ DatabaseHelper.DATABASE_COLUMN_EXPLANATION + " " + " FROM "
					+ DatabaseHelper.DATABASE_TABLE_NAME + " " + stringWhere, null);
			if (cursor != null && cursor.moveToFirst()) {
				do {
					soccer = new Soccer(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17), cursor.getString(18), cursor.getString(19), cursor.getString(20), cursor.getString(21), cursor.getString(22), cursor.getString(23));
					setSoccerAlarm(soccer);
					
				} while (cursor.moveToNext());
			}		
					
								
		} finally {
			if (cursor != null)
				cursor.close();
			if(databse != null){
				databse.close();
			}
		}
	}

	

	private void setSoccerAlarm(Soccer s) {
		// TODO Auto-generated method stub
		int key = s.getId();
		String day = s.getCompare_date();
		String hour = s.getReal_time().substring(0, 2);
		String minute = s.getReal_time().substring(3);
		
		int year = Integer.parseInt(s.getSetting_year());
		int month = Integer.parseInt(s.getSetting_month());
		int notification_time_value = Integer.parseInt(soccer.getNotification());
		long set_alarm_time = 0L;
		
		
		
		Intent intentS = new Intent(ALARM_SOCCER_ALERT_ACTION);
		intentS.putExtra("key", key);
		PendingIntent sender = PendingIntent.getBroadcast(mContext, key, intentS, 0);
		AlarmManager am = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		long cur_time = calendar.getTimeInMillis();
		
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DATE, Integer.parseInt(day));
		calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
		calendar.set(Calendar.MINUTE, Integer.parseInt(minute));
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		if(notification_time_value==0){
			set_alarm_time = calendar.getTimeInMillis();
		}else{
			set_alarm_time = calendar.getTimeInMillis()-Long.valueOf(notification_time_array_value[notification_time_value]);
		}
		
		if(cur_time>set_alarm_time){ 
			//현재 시작보다 설정한시간이 지난시간이면 알람 안울림
		}else{
			am.set(AlarmManager.RTC_WAKEUP, set_alarm_time, sender);
		}
		
	}



}
